
#函数
# def fib(max):
#     n,a,b=0,0,1
#     while n <max:
#         print(b)
#         a,b=b,a+b  #相当于 t = (b,a+b)   a = t[0]  a = t[1]
#         n = n+1
#     return 'done'
# fib(10)

#生成器generator 带有yield的关键字的函数为生成器
def fib(max):
    n,a,b = 0,0,1

    while n < max:
        yield b
        a,b = b,a+b
        n = n+1
    return 'done'
# a = fib(6)   类型为generator
# print(a)

# data = fib(10)
# print(data.__next__())
# print(data.__next__())  #生成器每次调用__next__()函数时，遇到yield语句返回，再次执行时从上次返回的yield语句处继续执行。
# print('执行别的命令')  #如果生成器没有数据在调用__next__()方法会报错。
# print(data.__next__())
# print(data.__next__())
# print(data.__next__())
# print('还能执行别的命令')
# print(data.__next__())
# print(data.__next__())
# print(data.__next__())

#生成器一般调用方法
g = fib(6)
while True:
    try:
        x = next(g)
        print('g',x)
    except StopIteration as e:
        print(e)
        break


